import Taro, { Component, Config } from '@tarojs/taro';
const QQMapWX = require('../../utils/qqmap-wx-jssdk');
// import { connect } from '@tarojs/redux';
import { View } from '@tarojs/components';

export interface HomeState {
  location: any;
  qqMapSdk: any;
}

class Index extends Component<{}, HomeState> {
  constructor(props: {}) {
    super(props);
    const qqMapSdk = new QQMapWX({ key: 'CI3BZ-W4NKU-O6EVO-BFXDY-S47O5-GFFJH' });
    this.state = {
      location: '',
      qqMapSdk: qqMapSdk
    };
  }

  config: Config = {
    navigationBarTitleText: '流年的樱花逝',
    navigationBarBackgroundColor: '#187b80'
  };

  componentDidMount() {
    const { qqMapSdk } = this.state;
    let _this = this;
    Taro.getLocation()
      .then(res => {
        qqMapSdk.reverseGeocoder({
          location: { latitude: res.latitude, longitude: res.longitude },
          success: function(res) {
            _this.setState({ location: res.result.address });
          },
          fail: function(res) {
            console.log(res);
          }
        });
      })
      .catch(error => {
        console.log(error);
      });
  }

  render() {
    const { location } = this.state;
    return <View>{location}</View>;
  }
}

export default Index;
